/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo1;

import Modelo.DAL.Sede_DAO;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONObject;

/**
 *
 * @author andres
 */
public class listaSedeAction extends org.apache.struts.action.Action {

  /* forward name="success" path="" */
  private static final String SUCCESS = "success";
  private static final String FAILURE = "failure";

  /**
   * This is the action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @throws java.lang.Exception
   * @return
   */
    @Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {

    String nombre = request.getParameter("nombre");
    String direccion = request.getParameter("direccion");
    String telefono = request.getParameter("telefono");
    String descripcion = request.getParameter("descripcion");
    String offset = request.getParameter("offset");
    String limit = request.getParameter("limit");

    String where = "SELECT *,(Select count(*) from sede) FROM sede where 1 = 1 ";

    if (nombre != null && !nombre.equals("")) {
      where += " and nombre_edificio = '" + nombre + "'";
    }

    if (direccion != null && !direccion.equals("")) {
      where += " and direccion = '" + direccion + "'";
    }

    if (telefono != null && !telefono.equals("")) {
      where += " and telefono = '" + telefono + "'";
    }

    if (descripcion != null && !descripcion.equals("")) {
      where += " and descripcion = '" + descripcion + "'";
    }
    where += "AND estado_sede = 'on'";
    where += " ORDER BY nombre";
    if (limit != null && !limit.equals("0")) {
      where += " limit " + limit + " offset " + offset;
    }
    System.out.println(where);
    response.setContentType("text/json");
    JSONObject ext = new JSONObject();
        Sede_DAO sDao = new Sede_DAO();
    //JSONObject sedes = Sede_DAO.consultarSedeTabla(where);
    //if (sedes == null) {
    //  return mapping.findForward(FAILURE);
    //}

   
    ext.accumulate("consultarSede", sDao.consultarSedeTabla(where));
   PrintWriter pw = response.getWriter();
        pw.write(ext.toString());
        pw.flush();
        pw.close();
    return mapping.findForward(SUCCESS);
  }
}


  